package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type LevelType struct{
	gorm.Model
	Name string
	LevelTypeID string
	ParentTypeID string
	CompanyID string
	Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email string `valid:"email"`
	Identity string
	ClientIP string
	ClientPort string
	Salt string
	LoginTime time.Time
	isLogout bool
	DeviceInfo string
 }

 func (table *LevelType) TableName() string {

	return "level_basic"
}

func GetLevelTypeList() []*LevelType { 
	data := make([]*LevelType,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindLevelTypeByName(name string,CompanyID string) LevelType {
	user := LevelType{}
	utils.DB.Where("name = ? and company_id = ?",name,CompanyID).Find(&user)
	return user
}

func FindLevelTypeByID(id uint,CompanyID string) LevelType {
	user := LevelType{}
	utils.DB.Where("id = ? and company_id = ?",id,CompanyID).First(&user)
	return user
}

func FindLevelTypeByIDAndCompanyID(id string,CompanyID string) LevelType {
	user := LevelType{}
	utils.DB.Where("level_type_id = ? and company_id = ?",id,CompanyID).First(&user)
	return user
}

func FindLevelTypeByCompanyID(CompanyID string) []*LevelType {
	user := make([]*LevelType,10)
	utils.DB.Where("company_id = ?",CompanyID).Find(&user)
	return user
}

func CreateLevelType(user LevelType) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteLevelType(user LevelType) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateLevelType(user LevelType) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(LevelType{Name: user.Name,DeviceInfo: user.DeviceInfo})
}